import { getMessageList } from "@/api/system/message"

const message = {
    state: {
        messageList: [],
        messageNum:0,
        messageUnread:0
      },

      mutations: {
        SET_MESSAGE_LIST: (state, messageList) => {
          state.messageList = messageList
        },
        SET_MESSAGE_NUM: (state, messageNum) => {
          state.messageNum = messageNum
        },
        SET_MESSAGE_UNREAD: (state, messageUnread) => {
          state.messageUnread = messageUnread
        }
      },

      actions: {
        SetMessageList({commit}) {
          return new Promise((resolve, reject) => {
            getMessageList({}).then(res => {
                const data = res.data
                commit('SET_MESSAGE_LIST', data.list)
                commit('SET_MESSAGE_NUM', data.num)
                commit('SET_MESSAGE_UNREAD', data.unread ? data.unread : 0)
                resolve()
            }).catch(error => {
                reject(error)
            })
          })
        },
    }
}

export default message
